<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/6/6 9:41
 * @copyright PanxSoft Inc.
 */

namespace App\Services\Presenters\Admin;


use App\Http\Controllers\Admin\Customer\WechatUserController;
use App\Models\Panx\SkinDetector;
use App\Models\Store\SkinRecord;
use App\Models\Store\WaxSkinRecord;
use App\Services\Presenters\WaxSkinData;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as BaseCollection;

class WaxSkinRecordPresenter
{
    /**
     * 指定店铺的 微信用户的 唯丝普接触图像检测仪 肤质数据 统计数据
     * 附加微信用户反查的筛选条件
     *
     * @param array $store_ids
     *
     * @param $customer_id
     * @return array
     */
    public static function storesUsersStatisticLevelGroupData(array $store_ids, $customer_id): array
    {
        /** @var Collection $latest_skin_records */
        $latest_skin_records = SkinRecord::latestStoresWechatUsersTypeSkinRecords(
            $store_ids, SkinDetector::ID_WAX
        );

        $skin_record_ids = $latest_skin_records->pluck('id')->all();

        /** @var Collection $wax_skin_records */
        $wax_skin_records = (new WaxSkinRecord)
            ->whereIn('skin_record_id', $skin_record_ids)
            ->whereIn('itemsid', WaxSkinRecord::ITEMSID_ALL)
            ->orderBy('itemsid', 'asc')
            ->get();

        // sid_groups
        $itemsid_group = $wax_skin_records->groupBy('itemsid');

        $position_counts = $itemsid_group
            ->map(function (Collection $wax_sid_group) {
                return $wax_sid_group->count();
            });

        $all_label_counts = $itemsid_group
            ->map(function (Collection $wax_sid_group) use($customer_id){
                return $wax_sid_group
                    ->groupBy(function (WaxSkinRecord $record) use($customer_id){

                        $wax_level_name = WaxSkinData::transScoreLevelNameByCustom($record->itemsid, $record->levelsort, $customer_id);

                        return $wax_level_name ?: trans('service.unknown');
                    })
                    ->transform(function (Collection $wax_level_name_group) {
                        return self::assembleLevelGroupData($wax_level_name_group);
                    });
            });

        // 这里是要求必须返回所有 itemsid 数据，没有的话对应项目也要返回空数组给前端
        $item_level_counts = collect(WaxSkinRecord::ITEMSID_ALL)
            ->map(function (int $itemsid) use ($all_label_counts) {
                return [
                    'itemsid' => $itemsid,
                    'level_counts' => $all_label_counts
                        ->get($itemsid, collect())  // 找对应的 itemsid, 如果没有直接返回 []
                        ->map(function (array $level_counts, string $level_name) {
                            return compact('level_name') + $level_counts;
                        })
                        ->values()
                ];
            });

        return compact(
            'position_counts',
            'all_label_counts',
            'item_level_counts'
        );
    }

    /**
     * 返回前端这样的数据才能通过饼图点击反查搜索对应的用户数据列表
     *
     * @used-by WechatUserController::reverseFilterWaxSidLvWxUsers()
     *
     * @param Collection|WaxSkinRecord[] $wax_level_name_group
     *
     * @return array
     */
    public static function assembleLevelGroupData(Collection $wax_level_name_group): array
    {
        $first = $wax_level_name_group->first();

        return [
            'levelsort' => $first->levelsort,
            'itemsid'   => $first->itemsid,
            'count'     => $wax_level_name_group->count()
        ];
    }
}